<template>
  <view class="agreement-mask" v-if="show" @click.stop="handleMaskClick">
    <view class="agreement-content" @click.stop>
      <view class="agreement-title">服务条款</view>
      <view class="agreement-body">
        <text class="agreement-text nolink" space="emsp">请您在使用GVR小程序前点击</text>
        <text class="agreement-text link" @click="openPrivacyPolicy">《GVR隐私权政策》</text>
        <text class="agreement-text link" @click="openUserAgreement">《GVR用户服务协议》</text>
        <text class="agreement-text link" @click="openWechatPrivacy">《微信小程序隐私保护政策》</text>
        <text class="agreement-text nolink">并仔细阅读，如您同意</text>
        <text class="agreement-text link" @click="openPrivacyPolicy">《隐私权政策》</text>
        <text class="agreement-text link" @click="openUserAgreement">《GVR用户服务协议》</text>
        <text class="agreement-text link" @click="openWechatPrivacy">《微信小程序隐私保护政策》</text>
        <text class="agreement-text nolink">的全部内容，请点击"同意并继续"开始使用我们的服务。</text>
      </view>
      <view class="agreement-buttons">
		  <button class="gvr-yellow-button" 
		          @click="handleSure">同意并继续</button>
		<!--  <button class="gvr-yellow-button" open-type="getPhoneNumber"
		          @getphonenumber="handleSure">同意并继续</button> -->
        <button class="gvr-gray-button" @click="handleCancel">不同意</button>
      </view>
    </view>
  </view>
</template>

<script setup>
import { ref } from 'vue'
import { userApi } from '@/service/api.js'
import { userStore } from '@/utils/userStore.js'
import { RouterMethods } from '@/utils/router'

const show = ref(false)
const emit = defineEmits(['confirm'])
const open = () => {
  show.value = true
}

const close = () => {
  show.value = false
}

const handleMaskClick = () => {
  // 点击蒙版不关闭
}

const handleCancel = () => {
  close()
}

const handleSure = async(e) => {
  close()
  const userInfo = await userApi.weixinCode()
  if (userInfo.code == undefined) {
	  userStore.saveUserInfo(userInfo)
	   uni.showToast({
			title:"登录成功",
			icon: 'none',
			duration:1000
	   })
	   emit('confirm')
	  // setTimeout(()=>{
		 //  emit('confirm')
	  // },1300)
  }
}

// 检查用户状态并处理跳转
// const checkUserStatus = () => {
// 	emit('confirm')
//   // 如果未选择角色，跳转到角色选择
//   if (userInfo.value.type != undefined) {
// 	  if (userInfo.value.type.length == 0 && isLoggedIn.value) {
// 		RouterMethods.toChooseRole()
// 		return
// 	  }
//   }
//  // 检查是否需要加入组织
//     if (!userInfo.value.member && 
//         (userInfo.value.type === USER_TYPES.GAS || 
//          userInfo.value.type === USER_TYPES.MAINTENANCE) && isLoggedIn.value) {
//       showJoinOrg()
// 	  return
//     }
	

// }

// const handleSure = async(e) => {
//   close()
//    if (e.detail.errMsg === 'getPhoneNumber:ok') {
// 	   try {
// 		 const userInfo = await userApi.weixinCode()
// 		 if (userInfo.code == undefined) {
// 			  if (userInfo.userInfo.phone == undefined) {
// 				  console.log('获取的手机code是：'+ e.detail.code)
// 				  const result = await userApi.updatePhone(e.detail.code)
// 				  if(result.code == undefined) {
// 					   console.log('更新手机号获取成功'+ JSON.stringify(result))
// 					   userStore.saveUserInfo(result)
// 					   uni.showToast({
// 									title:"登录成功",
// 									icon: 'none',
// 									duration:1000
// 					   })
// 					  setTimeout(()=>{
// 						RouterMethods.toChooseRole()
// 					  },1300)
// 				  } else {
// 					  throw new Error(result.msg || '上传手机号失败')
// 				  }
// 			  } else {
// 				  emit('confirm')
// 			  }
			 
			
// 		  } else {
// 			  throw new Error(userInfo.msg || '登录失败')
// 		  }
// 	   } catch (error) {
// 		 uni.showToast({
// 		   title: err.message || '登录失败',
// 		   icon: 'none'
// 		 })
// 	   }
//    } else {
// 	   uni.showToast({
// 	     title: '用户未授权手机号',
// 	     icon: 'none'
// 	   })
//    }
// }

// 打开各种协议
const openPrivacyPolicy = () => {
  uni.navigateTo({
    url: '/pages/agreement/privacy-policy'
  })
}

const openUserAgreement = () => {
  uni.navigateTo({
    url: '/pages/agreement/user-agreement'
  })
}

const openWechatPrivacy = () => {
  uni.navigateTo({
    url: '/pages/agreement/wechat-privacy'
  })
}
defineExpose({
  open,
  close
})
</script>

<style>
.agreement-mask {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  background: rgba(0, 0, 0, 0.61);
  z-index: 999;
  display: flex;
  align-items: center;
  justify-content: center;
}

.agreement-content {
  margin: 0 76rpx;
  background: #FFFFFF;
  padding: 60rpx 56rpx;
}

.agreement-title {
 font-family: PingFangSC-Medium;
 font-size: 36rpx;
 color: #000000;
 letter-spacing: 0;
 text-align: center;
 font-weight: 500;
  margin-bottom: 32rpx;
}

.agreement-body {
  margin-bottom: 52rpx;
}

.agreement-text {
  font-family: PingFangSC-Thin;
  font-size: 28rpx;
  letter-spacing: 0;
  text-align: justify;
  line-height: 52rpx;
  font-weight: 200;
}
.nolink {
 color: #B5B5B5;
}

.link {
  color: #285789;
}

.agreement-buttons {
  display: flex;
  flex-direction: column;
  margin-top: 52rpx;
  width: 470rpx;
  height: 180rpx;
  justify-content: space-between;
}

.agreement-buttons button::after {
  border: none;
}

</style> 